package lumis.portal.incontextedit;

import lumis.portal.PortalException;
import lumis.portal.controller.ControllerException;
import lumis.portal.controller.ControllerXml;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinterfaceinstance.ServiceInterfaceInstanceConfig;
import lumis.portal.transaction.PortalTransactionFactory;
import lumis.util.ITransaction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

/**
 * Controller for in context edit.
 * 
 * @version $Revision: 13214 $ $Date: 2011-07-07 21:46:10 -0300 (Thu, 07 Jul 2011) $
 * @since 6.0.1
 */
public class InContextEditControllerXml extends ControllerXml
{
	public InContextEditControllerXml(HttpServletRequest request, HttpServletResponse response, PageContext pageContext) throws ControllerException, PortalException
	{
		super(request, response);
	}

	public void handleRequest() throws ControllerException, Exception
	{
		String itemId = request.getParameter("itemId");
		String interfaceInstanceId = request.getParameter("ii");
		if (itemId != null)
			handleContentRequest(itemId, request.getParameter("lumPageId"));
		else if (interfaceInstanceId != null)
			handleInterfaceInstanceRequest(interfaceInstanceId, request.getParameter("lumItemId"));
		else
		{
			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
			out.write(getResponseXml(new PortalException("STR_BAD_IN_CONTEXT_EDIT_REQUEST")));
		}
	}

	/**
	 * Handles requests for content in context edit menus.
	 * 
	 * @param itemId
	 *            the item id of menu being requested.
	 * @since 6.0.1
	 */
	public void handleContentRequest(String itemId, String sourcePageId) throws Exception
	{
		try
		{
			ITransaction transaction = PortalTransactionFactory.createTransaction();
			try
			{
				transaction.begin();
				IServiceInterfaceInContextEdit serviceInterfaceInContextEdit = ServiceInterfaceInContextEditFactory
						.getDefaultServiceInterfaceInContextEdit();

				String menu = serviceInterfaceInContextEdit.getInContextEditMenu(sessionConfig,
						itemId, sourcePageId, transaction);
				if (menu != null)
					out.write(menu);

				transaction.commit();
			}
			catch (Exception e)
			{
				transaction.rollback();
				throw e;
			}
			finally
			{
				transaction.dispose();
			}
		}
		catch (PortalException e)
		{
			out.write(getResponseXml(e));
		}
		catch (Exception e)
		{
			PortalException portalException = new PortalException(e.getMessage(), e);
			out.write(getResponseXml(portalException));
		}

	}

	/**
	 * Handles requests for interface instances in context edit menus.
	 * 
	 * @param interfaceInstanceId
	 *            the interface instance id of menu being requested.
	 * @since 6.0.1
	 */
	public void handleInterfaceInstanceRequest(String interfaceInstanceId, String lumItemId)
			throws Exception
	{
		try
		{
            ITransaction transaction = PortalTransactionFactory.createTransaction();
            try
            {
            	transaction.begin();
            	
				ServiceInterfaceInstanceConfig serviceInterfaceInstanceConfig = ManagerFactory
						.getServiceInterfaceInstanceManager().get(sessionConfig,
								interfaceInstanceId, transaction);
            	
            	IServiceInterfaceInContextEdit serviceInterfaceInContextEdit = ServiceInterfaceInContextEditFactory
						.getDefaultServiceInterfaceInContextEdit();
				String menu = serviceInterfaceInContextEdit
						.getInContextEditMenu(sessionConfig, serviceInterfaceInstanceConfig,
								(lumItemId != null && "".equals(lumItemId) ? null : lumItemId),
								transaction);
				if (menu != null)
					out.write(menu);

				transaction.commit();
            }
            catch(Exception e)
            {
            	transaction.rollback();
            	throw e;
            }
			finally
			{
				transaction.dispose();
			}
		}
		catch(PortalException e)
		{
			out.write(getResponseXml(e));
		}
		catch(Exception e)
		{
			PortalException portalException = new PortalException(e.getMessage(), e);
			out.write(getResponseXml(portalException));
		}
	}
}
